<template>
  <el-row class="warp">
    <el-col :span="24" class="warp-main">
      <!--工具条-->
      <div class="header-wrap">
        <el-form :inline="true" :model="filters" label-width="100px" size="medium">
          <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form-item label="角色名称：">
              <el-input v-model="filters.name" placeholder="请输入关键字搜索"></el-input>
            </el-form-item>
            <el-form-item label="启用状态：">
              <el-select v-model="filters.status" placeholder="请选择" @change="getSearchStatus">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-on:click="getRoles">搜索</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="showAddDialog">新增</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </div>
      <!--列表-->
      <el-table :data="getAllRoles" :border="true" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="index" align="center" ></el-table-column>
        <el-table-column prop="name" label="角色名称" align="center"></el-table-column>
        <el-table-column prop="remark" label="角色描述" align="center"></el-table-column>
        <el-table-column label="启用状态" align="center">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.status == 0">有效</el-tag>
            <el-tag type="danger" v-if="scope.row.status == 1">无效</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showEditDialog(scope.$index,scope.row)">编辑</el-button>
            <el-button type="danger" @click="deleteRole(scope.$index,scope.row)" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div style="float:right;margin-top:15px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[10, 15, 20, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount">
        </el-pagination>
      </div>

      <!--新增界面-->
      <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false" width="70%" >
        <el-form :model="addForm" :rules="addFormRules" ref="addForm" label-width="100px">
          <el-form-item label="角色名称:" prop="name">
            <el-input v-model="addForm.name" placeholder="请输入角色名称" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述:" prop="remark">
            <el-input v-model="addForm.remark" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="权限设置:">
            <el-tree
              :data="getMenuData"
              :props="defaultProps"
              show-checkbox
              default-expand-all
              node-key="id"
              ref="tree"
              highlight-current
              @current-change="handleCurrentClick">
            </el-tree>
          </el-form-item>
          <el-form-item label="启用状态:">
            <el-radio v-model="addForm.status" label="0">有效</el-radio>
            <el-radio v-model="addForm.status" label="1">无效</el-radio>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click="addSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>

      <!-- 编辑 -->
      <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false" width="70%">
        <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
          <el-form-item label="角色名称:">
            <el-input v-model="editForm.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述:">
            <el-input v-model="editForm.remark" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="权限设置:">
            <el-tree
              :data="getMenuData"
              :props="defaultProps"
              :default-checked-keys="checkedItems"
              show-checkbox
              default-expand-all
              node-key="id"
              ref="tree"
              highlight-current
              @current-change="handleCurrentClick">
            </el-tree>
          </el-form-item>
          <el-form-item label="启用状态:">
            <el-radio-group v-model="editForm.status">
              <el-radio class="radio" :label="0">有效</el-radio>
              <el-radio class="radio" :label="1">无效</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editFormVisible = false">取消</el-button>
          <el-button type="primary" @click="editSubmit" :loading="editLoading">提交</el-button>
        </div>
      </el-dialog>

    </el-col>
  </el-row>
</template>
<script type="text/ecmascript-6">
  import {fetch, post} from '@/utils/http'
  export default{
    data(){
      return {
        props: {
          value: 'id',
          children: 'resources',
          label: 'name'
        },
        checkId:1,
        getMenuData:[],
        defaultProps: {
          children: 'resources',
          label: 'name'
        },
        selectDta:[],
        options: [{
          value: '',
          label: '全部'
        }, {
          value: '0',
          label: '有效'
        }, {
          value: '1',
          label: '无效'
        }],
        filters: {
          name: '',
          status:''
        },
        status: 0,
        totalCount: 0,
        pageIndex: 1,
        pageSize: 10,
        listLoading: false,
        sels: [], //列表选中列
        getAllRoles: [],
        checkedItems:[],
        //编辑相关数据
        editFormVisible: false,//编辑界面是否显示
        editLoading: false,
        editFormRules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'}
          ],
          remark: [
            {required: true, message: '请输入描述', trigger: 'blur'}
          ]
        },
        editForm: {
          name: '',
          remark: '',
          resourceIds: [],
          status: ''
        },

        //新增相关数据
        addFormVisible: false,//新增界面是否显示
        addLoading: false,
        addFormRules: {
          name: [
            {required: true, message: '请输入角色名称', trigger: 'blur'}
          ],
          remark: [
            {required: true, message: '请输入角色描述', trigger: 'blur'}
          ]
        },
        addForm: {
          name: '',
          remark: '',
          resourceIds: [],
          status: '0'
        }
      }
    },
    methods: {
      // getCheckedNodes() {
      //   console.log(this.$refs.tree.getCheckedNodes());
      // },
      // 下拉框选择内容改变后筛选
      selectChange:function(){
      },
      selsChange: function (sels) {
        this.sels = sels;
      },
      //显示编辑界面
      showEditDialog: function (index, row) {
        this.editFormVisible = true;
        this.getMenuD();
        this.editForm = Object.assign({}, row);
        console.log(this.editForm);
        //编辑页面当前的角色处于选中状态
        this.checkedItems = this.editForm.resourceIds;
      },
      //编辑
      editSubmit: function () {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              let para = Object.assign({}, this.editForm);
              para.resourceIds = this.$refs.tree.getCheckedKeys();
              post('/api/manager/v1/role/updateRole',para).then((response) => {
                this.editLoading = false;
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['editForm'].resetFields();
                this.editFormVisible = false;
                this.getRoles();
              });
            });
          }
        });
      },
      //编辑、新增弹出框里的菜单数据
      getMenuD: function(){
        post('/api/manager/v1/resource/find',{}).then((response) => {
          this.getMenuData = response.data;
          console.log(this.getMenuData)
          this.listLoading = false;
        }).catch(function(err) {
          console.log(err)
        });
      },
      //点击节点，获取当前节点信息
      handleCurrentClick(getMenuData, node) {
        this.checkId = getMenuData.id
      },

      //新增--加载菜单数据
      showAddDialog: function () {
        //新增内容弹框显示
        this.addFormVisible = true;
        this.listLoading = true;
        let _this = this;
        this.getMenuD();
        this.addForm = {
          name: '',
          remark: '',
          resourceIds: [],
          status: '0'
        };
      },
      //新增
      addSubmit: function () {
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              let _this = this;
              // console.log(this.$refs.tree.getCheckedKeys());
              this.addLoading = true;
              let para = Object.assign({}, this.addForm);
              //获取当前选中的权限的id,给resourceIds，resourceIds是数组
              para.resourceIds = this.$refs.tree.getCheckedKeys();
              post('/api/manager/v1/role/addRole',para).then((response) => {
                this.addLoading = false;
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.addFormVisible = false;
                this.getRoles();
              });
            });
          }
        });
      },
      getSearchStatus(value) {
        // this.getRoles();
      },
      //获取用户列表
      getRoles() {
        let para = {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
        };
        if(!this.filters.name.replace(/\s/g, "") == '') {
          para.name = this.filters.name.replace(/\s/g, "")
        }
        if(!this.filters.status == '') {
          para.status = this.filters.status
        }
        this.listLoading = true;
        let _this = this;
        post('/api/manager/v1/role/queryRole',para).then((response) => {
          this.getAllRoles = response.data.data;
          this.totalCount = response.data.totalCount
          this.listLoading = false;
        }).catch(function(err) {
          console.log(err)
        });
      },
      //根据角色搜索
      /*searchRoles() {
        let para = {
          pageIndex: this.page,
          pageSize: this.pageSize,
          name: this.filters.name
        };
        this.listLoading = true;
        let _this = this;
        post('/api/manager/v1/role/queryRole',para).then((response) => {
          this.getAllRoles = response.data.data;
          this.totalCount = response.data.totalCount
          this.listLoading = false;
        }).catch(function(err) {
          console.log(err)
        });
      },*/
      //删除
      deleteRole: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {type: 'warning'}).then(() => {
          this.listLoading = true;
          let para = {id: row.id};
          post('/api/manager/v1/role/deleteRole',para).then((response) => {
            this.listLoading = false;
            if(response.data.success == 2){
              this.$message({
                message: response.data.msg,
                type: 'error'
              });
            }else{
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }
            this.getRoles();
          }).catch(function(err) {
            console.log(err)
          });
        }).catch(() => {
          // 超时
        });
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.getRoles()
      },
      handleCurrentChange(val) {
        this.pageIndex = val;
        this.getRoles();
      },
    },

    mounted() {
      this.getRoles();

    }
  }
</script>

<style scoped>
  .header-wrap{
    height: 60px;
    padding-top: 20px;
    margin-bottom: 60px;
    background-color: #66BFBF;
    border-radius: 6px;
  }
  .demo-table-expand label {
    font-weight: bold;
  }
  .el-form-item label{
    padding-right: 50px;
  }
</style>
